package com.gs.SynPatrolRep.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.util.DateUtil;

import java.util.Calendar;
import java.util.Date;

public class LabelDatePicker extends LinearLayout {
    private TextView item_lbl;
    private TextView item_text;
    private String format;
    private Date mDate;
    private Calendar calendar;

    public String getLbl() {
        return item_lbl.getText().toString();
    }

    public void setLbl(String text) {
        item_lbl.setText(text);
    }

    public void setLblHint(String hint) {
        item_lbl.setHint(hint);
    }

    public String getText() {
        return item_text.getText().toString();
    }

    public void setText(String text) {
        item_text.setText(text);
    }

    public Date getDate() {
        return mDate;
    }

    public void setDate(Date date) {
        this.mDate = date;
        if (date != null) {
            String text = DateUtil.getInstance().DateTimeToStr(date);
            item_text.setText(text);
        } else {
            item_text.setText("");
        }
    }

    public void setTextHint(String hint) {
        item_text.setHint(hint);
    }

    public LabelDatePicker(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        calendar = Calendar.getInstance();
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDate != null) {
                    calendar.setTime(mDate);
                }
//                DateUtil.getInstance().showDateSelector(context, v, calendar, new DateUtil.ShowTimeSelectorCallback() {
//                    @Override
//                    public void OnTimeSelectListener(Date date) {
//                        mDate = date;
//                        calendar.setTime(date);
//                        item_text.setText(DateUtil.getInstance().DateTimeToStr(date, format));
//                    }
//                });
            }
        });
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.LabelDatePicker);
        LayoutInflater.from(context).inflate(R.layout.label_date_picker, this, true);
        item_lbl = findViewById(R.id.item_lbl);
        item_text = findViewById(R.id.item_text);
        item_text.setText("");
        item_lbl.setText("");
        format = attributes.getString(R.styleable.LabelDatePicker_label_date_picker_format);
        if (format == null || TextUtils.isEmpty(format)) {
            format = "yyyy-MM-dd";
        }
        String text = attributes.getString(R.styleable.LabelDatePicker_label_date_picker_edit_text);
        String textHint = attributes.getString(R.styleable.LabelDatePicker_label_date_picker_edit_text_hint);
        boolean isStar = attributes.getBoolean(R.styleable.LabelDatePicker_label_date_picker_star, false);
        item_text.setText(text);
        item_text.setHint(textHint);
        String textVisibility = attributes.getString(R.styleable.LabelDatePicker_label_date_picker_visibility);
        if (textVisibility != null && !TextUtils.isEmpty(textVisibility)) {
            switch (textVisibility) {
                case "visible":
                    item_text.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_text.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_text.setVisibility(GONE);
                    break;
            }
        }
        String lbl = attributes.getString(R.styleable.LabelDatePicker_label_date_picker_lbl);
        if (isStar) {
            Spannable spannable = new SpannableString("* " + lbl);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.deeper_red)), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            item_lbl.setText(spannable);
        } else {
            item_lbl.setText(lbl);
        }
        String lblVisibility = attributes.getString(R.styleable.LabelDatePicker_label_date_picker_lbl_visibility);
        if (lblVisibility != null && !TextUtils.isEmpty(lblVisibility)) {
            switch (lblVisibility) {
                case "visible":
                    item_lbl.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_lbl.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_lbl.setVisibility(GONE);
                    break;
            }
        }
        attributes.recycle();
    }
}
